package suzf.top.base.ui.activity

import android.os.Bundle
import android.os.PersistableBundle
import org.jetbrains.anko.toast
import suzf.top.base.common.BaseApplication
import suzf.top.base.injection.component.ActivityComponent
import suzf.top.base.injection.component.DaggerActivityComponent
import suzf.top.base.injection.module.ActivityModule
import suzf.top.base.injection.module.LifecycleProviderModule
import suzf.top.base.presenter.BasePresenter
import suzf.top.base.presenter.view.BaseView
import suzf.top.base.widgets.ProgressLoading
import javax.inject.Inject

/**
 *
 *========================
 * Created with AndroidStudio
 * Author:Mr.Su
 * Date:2019-09-07
 * Time:20:49
 * ========================
 */
open abstract class BaseMvpActivity<T: BasePresenter<*>>: BaseActivity(),BaseView {

    abstract fun injectComponent()

    @Inject
    lateinit var mPresenter: T

    lateinit var activityComponent: ActivityComponent

    private lateinit var mLoadingDialog: ProgressLoading

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initActivityInjection()
        injectComponent()

        mLoadingDialog = ProgressLoading.create(this)
    }

    private fun initActivityInjection() {
        activityComponent = DaggerActivityComponent.builder()
                .appComponent((application as BaseApplication).appComponent)
                .activityModule(ActivityModule(this))
                .lifecycleProviderModule(LifecycleProviderModule(this))
                .build()
    }

    override fun showLoading() {
        mLoadingDialog.showLoading()
    }

    override fun hideLoading() {
        mLoadingDialog.hideLoading()
    }

    override fun onError(text: String) {
        toast(text)
    }
}